package com.example.superoa.model.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.superoa.model.dao.SalaryCountDao;
import com.example.superoa.model.dao.SalaryItemDao;
import com.example.superoa.model.mappers.ItemsMapper;
import com.example.superoa.model.pojos.SalaryAccout;
import com.example.superoa.model.pojos.SalaryCount;
import com.example.superoa.model.pojos.SalaryItem;
import com.example.superoa.model.pojos.SalarySub;

@Service
@Transactional(rollbackFor=Exception.class)
public class ItemsService {
	@Autowired
	private ItemsMapper im;
	@Autowired
	private SalaryItemDao sid;
	@Autowired
	private SalaryCountDao scd;
	public JSONArray showItems() {
		return im.showItems();
	}
	public JSONArray findname(String items_name) {
		return im.finditemsname(items_name);
	}
	//新增
	public void xzitems(SalaryItem si,SalaryCount sc) {
		SalaryItem sis=new SalaryItem();
		int countid=im.count_id()+1;
		sis.setItemsId(countid);
		sis.setItemsName(si.getItemsName());		
		sis.setItemsType(si.getItemsType());
		sis.setSiRemarks(si.getSiRemarks());
		sid.save(sis);
		
		SalaryCount scs=new SalaryCount();
		scs.setScExplain(sc.getScExplain());
		scs.setScFormula(sc.getScFormula());
		scs.setScDanwei(sc.getScDanwei());
		SalaryItem count = sid.findById(countid).get();
		scs.setSalaryItem(count);
		scd.save(scs);
		
	}
	//编辑
	public void bianji(int items_id,int count_id,String itemsName,String itemsType,String itemsSm,BigDecimal sc_formula,String sc_explain,String sc_danwei) {
		SalaryItem sis = sid.findById(items_id).get();
		sis.setItemsName(itemsName);
		sis.setItemsType(itemsType);
		sis.setSiRemarks(itemsSm);
		sid.save(sis);
		SalaryCount sc=scd.findById(count_id).get();
		sc.setScExplain(sc_explain);
		sc.setScDanwei(sc_danwei);
		sc.setScFormula(sc_formula);
		scd.save(sc);
	} 
	//初始化
	public JSONArray selectid(int items_id) {
		return im.finditemsid(items_id);
	}
	//删除
	public void delect(int items_id) {
		//删除项目
		SalaryItem si = sid.findById(items_id).get();
		List<SalaryAccout> sa=si.getSalaryAccouts();
		if(sa==null) {
			sa=new ArrayList<>();
		}
		sa.clear();	
		si.setSalaryAccouts(sa);
		for (SalaryCount entity : si.getSalaryCounts()) {
			scd.deleteById(entity.getCountId());
		}
		sid.deleteById(items_id);
	} 
}
